<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class Pelajaran extends CI_Controller
{
    public function index()
    {
        $data['rslt_data_pelajaran'] = $this->db->select('mata_pelajaran.id, nama_pelajaran, nama_guru, keterangan, mata_pelajaran.nilai_min')
                ->from('mata_pelajaran')
                ->join('data_guru', 'data_guru.id = mata_pelajaran.kode_guru', 'INNER')
                ->join('tabel_pelajaran', 'tabel_pelajaran.kode_pelajaran = mata_pelajaran.kode_pelajaran', 'INNER')
                ->order_by('nama_pelajaran', 'ASC')->get();
        $data['r_guru'] = $this->db->query('SELECT id, nama_guru FROM data_guru ORDER BY nama_guru ASC');
        $this->template->admin('admin/v_pelajaran', $data, 2, "Data Pelajaran");
    }
	
	public function tambah()
	{
		$this->load->library('form_validation');
		$this->_rule_validation();
		if ($this->form_validation->run() === FALSE)
		{
			$data['r_guru'] = $this->db->query('SELECT id, nama_guru FROM data_guru ORDER BY nama_guru ASC');
			$data['r_pelajaran'] = $this->db->query('SELECT * FROM tabel_pelajaran ORDER BY nama_pelajaran ASC');
			$data['id'] = '';
			$data['kode_pelajaran'] = '';
			$data['kode_guru'] = '';
			$data['nilai_min'] = '';
			$data['keterangan'] = '';
			$this->template->admin('admin/v_pelajaran_ubah', $data, 2, "Form Tambah Data Pelajaran");
		}
		else
		{
			$this->_simpan('TAMBAH');
		}
	}
    
    public function edit($id = null)
    {
        if($id === null)
        {
            show_404();
        }
        else
        {
            $rslt_data = $this->db->select('*')->from('mata_pelajaran')->where('id', $id)->limit(1)->order_by('id', 'ASC')->get();
            if($rslt_data->num_rows() == 1)
            {
				$this->load->library('form_validation');
				$this->_rule_validation();
				if ($this->form_validation->run() === FALSE)
				{
					$rslt_data = $rslt_data->row();
					$data['r_guru'] = $this->db->query('SELECT id, nama_guru FROM data_guru ORDER BY nama_guru ASC');
					$data['r_pelajaran'] = $this->db->query('SELECT * FROM tabel_pelajaran ORDER BY nama_pelajaran ASC');
					$data['id'] = $rslt_data->id;
					$data['kode_pelajaran'] = $rslt_data->kode_pelajaran;
					$data['kode_guru'] = $rslt_data->kode_guru;
					$data['nilai_min'] = $rslt_data->nilai_min;
					$data['keterangan'] = $rslt_data->keterangan;
					$this->template->admin('admin/v_pelajaran_ubah', $data, 2, "Form Ubah Data Pelajaran");
				}
				else
				{
					$this->_simpan('UBAH');
				}
            }
            else
            {
                show_404();
            }
        }
    }
    
    public function hapus($id = null)
    {
        if($id === null)
        {
            show_404();
        }
        else
        {
            $this->db->where('id', $id)->limit(1)->delete('mata_pelajaran');
            redirect('admin/pelajaran');
        }
    }
    
    private function _simpan($proses = null)
    {
        if($proses == null)
        {
            show_404();
        }
        else
        {
            $id_guru = $this->input->post('id_guru');
            $kode_pelajaran = $this->input->post('kode_pelajaran');
            $nilai_min = $this->input->post('nilai_min');
            $keterangan = $this->input->post('keterangan');
			$datasimpan = array('kode_guru' => $id_guru,
                                'kode_pelajaran' => $kode_pelajaran,
                                'nilai_min' => $nilai_min,
                                'keterangan' => $keterangan);
            if($proses == 'TAMBAH')
            {
                    $this->db->insert('mata_pelajaran', $datasimpan);
            }
            else if($proses == 'UBAH')
            {
                $id = $this->input->post('id');
                if($id != '')
                {
                    $this->db->set($datasimpan)
                        ->where('id', $id)
                        ->update('mata_pelajaran');
                }
            }
            redirect('admin/pelajaran');
        }
    }
	
	private function _rule_validation()
	{
		$this->form_validation->set_message('required', '%s harap di isi');
		$this->form_validation->set_rules('id_guru', 'Guru', 'required|is_natural_no_zero');
		$this->form_validation->set_rules('kode_pelajaran', 'Nama Pelajaran', 'required|max_length[20]|trim');
		$this->form_validation->set_rules('nilai_min', 'Nilai Minimal', 'required|is_natural_no_zero');
		$this->form_validation->set_rules('keterangan', 'Keterangna', 'max_length[50]|trim');
	}
}

/* End of file pelajaran.php */
/* Location: ./application/controllers/admin/pelajaran.php */